package Model;


import java.util.*;

public class DeleteSubTaskCommand implements Command
{
   //attributes
   private ComplexTask ParentTask;
   private ArrayList<Task> TasksPointer;
   private Task DeletedSubTask;
   private boolean undone;
   private Task DeletedParentTask;

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public DeleteSubTaskCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
		DeletedSubTask = null;
        undone=false;
	}

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void execute(String taskName) throws Exception
    {
    	System.out.println("SUB TASK TO BE DELETED IS NAME OF: " +taskName);
    	if(taskName != null)
    	{
    		for(int i = 0; i < TasksPointer.size(); ++i)
			{
    			Iterator<Task> iteratesThroughParentTaskOfTree = TasksPointer.get(i).iterator();
    			Iterator<Task> iteratesThroughParentTaskOfTree2 = TasksPointer.get(i).iterator(); //this iterator will always be 1-step behind.
				TaskTreeIterator temp;
				TaskTreeIterator temp2;
				temp = (TaskTreeIterator)iteratesThroughParentTaskOfTree;
				temp2 = (TaskTreeIterator)iteratesThroughParentTaskOfTree2;
				Task currentInTree=null;
				//Task currentInTree2=null;
				Task previousTaskInSearch=null;
				TasksPointer.get(i).mTodo.size();
				boolean laststepofDoWhile=false;
				boolean laststepofDoWhile2=true;
				int loopCounter=0;
				//int max
    		
				do // go through entire Tree in search for ParentTask Name
				{
					if(laststepofDoWhile)
					{
						laststepofDoWhile2=false;
					}
					
					currentInTree = temp.getCurrent();

					System.out.println("-");
					System.out.println("Checking Element:" +currentInTree.getName());
										
					if(previousTaskInSearch == null && currentInTree.getName().equals(taskName))
					{
						TasksPointer.get(i).mTodo.remove(currentInTree);
						return;
					}
					
					else if(currentInTree.getName().equals(taskName)) 
					{
						//DeletedSubTask = currentInTree;
						//DeletedParentTask = previousTaskInSearch;
						//previousTaskInSearch.mTodo.remove(currentInTree);
						
						//------
						
						while(true)
						{
							Task loopTask = temp2.getCurrent();
							
							if(loopTask.mTodo.contains(currentInTree))
							{
								System.out.println("REMOVED...");
								DeletedSubTask = currentInTree;
								DeletedParentTask = loopTask;
								loopTask.mTodo.remove(currentInTree);
								return;
							}
							
							else
							{
								temp2.next();
							}
						}
						
						
						//------
						//return;
					}
					
					if(previousTaskInSearch == null)
					{
						previousTaskInSearch = currentInTree;
					}

					if(temp.hasNext())
					{
						System.out.println("moving to next...");
						temp.next();

						if(temp.hasNext()==false)
						{
							laststepofDoWhile=true;
						}
					}
					
					else
					{
						laststepofDoWhile2=false;
					}
					
				}while(laststepofDoWhile2); //end of do-while-loop
    		
			}//end of for-loop	
    	}
    	
    	if(DeletedSubTask !=null)
    	{
    		throw new Exception ("Sub Task Was Not Deleted!");
    	}
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void undo()
    {
        if(DeletedParentTask != null && undone==false)
		{
        	ComplexTask temp = (ComplexTask)DeletedParentTask;
        	temp.addTodoItem(DeletedSubTask);
            undone=true;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    public void redo()
    {
        if(DeletedParentTask != null && undone==true)
		{
        	ComplexTask temp = (ComplexTask)DeletedParentTask;
            temp.mTodo.remove(DeletedSubTask);
            
            //removeTask(DeletedSubTask);
            undone=false;
        }
    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

}
